/**
 * Created by shenhai on 15/10/15.
 */


var RoleApplyService = require('../../../services/mis/appuser/roleApplyService');
var messageUtil = require('../../../common/utils/messageUtil');
var toolUtil = require('../../../common/utils/toolUtil');
var mysqlUtil = require('../../../common/utils/mysqlUtil');
var moment = require('moment');


exports.roleApplyList = function(req, res) {

    RoleApplyService.roleApplyListService(req, function(err, results) {

        if (err) {
            logger.error('Exception happen when roleApplyList...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when roleApplyList...'));
        }else {
            var data = {
                count:results[0]["count"],
                totalPage: results[0]["totalPage"],
                list: results[1]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }

    });
};




exports.roleApplyStatusUpdate = function(req, res) {

    RoleApplyService.roleApplyStatusService(req, function(err, results) {

        if (err) {
            logger.error('Exception happen when roleApplyStatusUpdate...' + err.message);
            <!--  宝宝树66小区二次改造    add   gct  20170807   start   -->
            if (err.status == 999 || err.status == 888) {
                res.json(messageUtil.composeSucceededMessage(req, null, err));
            } else {
                res.json(messageUtil.composeFailedMessage(req, 'Exception happen when roleApplyStatusUpdate...'));
            }
            <!--  宝宝树66小区二次改造    add   gct  20170807   end   -->
        }else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    });
};

/*  宝宝树66小区二次改造    mod   chenf  20170802  start   */
/*小区管理权限设置*/
exports.roleApplyPowerSet = function(req, res) {

    RoleApplyService.roleApplyPowerSet(req, function(err, results) {

        if (err) {
            logger.error('Exception happen when roleApplyPowerSet...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when roleApplyPowerSet...'));
        }else {
            var data = {
                communityList:results[0]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    });
};

/*保存小区分配信息*/
exports.saveCommunitySet = function(req, res) {

    RoleApplyService.saveCommunitySet(req, function(err, results) {

        if (err) {
            if(err.status==999)
            {
                res.json(messageUtil.composeSucceededMessage(req, null,err));
            }
            else {
                logger.error('Exception happen when roleApplyPowerSet...' + err.message);
                res.json(messageUtil.composeFailedMessage(req, 'Exception happen when roleApplyPowerSet...'));
            }
        }else {
            res.json(messageUtil.composeSucceededMessage(req, null, {}));
        }
    });
};

/*编辑小区分配信息*/
exports.editCommunitySet = function(req, res) {

    RoleApplyService.editCommunitySet(req, function(err, results) {

        if (err) {
            logger.error('Exception happen when roleApplyPowerSet...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when roleApplyPowerSet...'));
        }else {
            res.json(messageUtil.composeSucceededMessage(req, null, {}));
        }
    });
};

// 检索可以分配的小区信息
exports.villageService=function(req,res)
{
    RoleApplyService.villageService(req, function(err, results) {

        if (err) {
            logger.error('Exception happen when regionCounty...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when regionCounty...'));
        }else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }

    });
};
/*  宝宝树66小区二次改造    mod   chenf  20170802  end   */